Ext.define('NIM.view.AssetChange.RelocationSite', {
      extend : 'Ext.panel.Panel',
      alias : 'widget.RelocationSite',
      id : 'relocationsite',
      border : false,
      title : 'Relocation Site',
      layout : 'fit',
      closable : true,
      layout : {
        type : 'border'
      },
      width : 1 * screen.width,
      height : 590,
      items : [{
            xtype : 'form',
            id : 'panelrelocationsite',
            title : 'Relocation Site Form',
            region : 'west',
            margin : '5 5 5 5',
            autoScroll:true,
            method : 'POST',
            url : './Services/AssetChange/Site/Change/Add',
            collapsible : true,
            collapsed : true,
            width : 600,
            items : [
                 {

	                  xtype : 'hiddenfield',
	                  cls : 'black',
	                  //id : 'relcoationsiteid',
	                  name : 'CHANGE_CATEGORY',
	                  value :'RELOCATION_SITE',
	                  margin : '5 10 0 25',
	                  labelWidth : 100 / 1366 * screen.width,
	                  width : 300 / 1366 * screen.width,
	                  fieldLabel : 'Site ID',
	                  msgTarget : 'title',
	                  allowBlank : true,
	                  validateBlank : false

                },
				{
	                  xtype : 'fieldcontainer',
	                  cls : 'black',
	                  margin : '10 10 10 10',
	                  fieldLabel : 'SITE ID',
	                  labelWidth : 130 / 1366 * screen.width,
	                  width : 325 / 1366 * screen.width,
	                  layout : 'hbox',
	                  items : [
	                     {
	                        xtype : 'textfield',
	                        id : 'relocationsiteid',
	                        name : 'SOURCE_SITE_ID',
	                        margin : '0 5 0 0',
	                        width : 135,
	                        hideLabel : true,
	                        allowBlank : false
	                        
	                      }, 
	                      {
	                        xtype : 'button',
	                        id : 'buttonsiteidfrom',
	                        text : 'Browse',
	                        width : 50,
	                        handler : function() {
	                          openPopup("relocationsiteid", "relocationsitename");
	
	                       }}]
                }, 
                {
	                  xtype : 'textfield',
	                  fieldLabel : 'SITE NAME',
	                  id : 'relocationsitename',
	                  labelWidth : 130 / 1366 * screen.width,
	                  width : 325 / 1366 * screen.width,
	                  margin : '5 10 0 10',
	                  allowBlank : false
                  
                },
                {
	                  xtype : 'datefield',
	                  minDate : new Date(),
	                  fieldLabel : 'DATE',
	                  name : 'CHANGE_DATE',
	                  // id:'relocationsitedate',
	                  labelWidth : 130 / 1366 * screen.width,
	                  width : 325 / 1366 * screen.width,
	                  margin : '5 10 0 10',
	                  allowBlank : false
                  
                }, 
                {
	                  xtype : 'fieldcontainer',
	                  cls : 'black',
	                  margin : '5 10 0 10',
	                  fieldLabel : 'TO SITE',
	                  labelWidth : 130 / 1366 * screen.width,
	                  width : 325 / 1366 * screen.width,
	                  layout : 'hbox',
	                  items : [{
	                        xtype : 'textfield',
	                        id : 'relocationsiteidto',
	                        margin : '0 5 0 0',
	                        name : 'DESTINATION',
	                        width : 135,
	                        hideLabel : true,
	                        allowBlank : false
	                      }, 
	                      {
	                        xtype : 'button',
	                        id : 'buttonsiteidto',
	                        text : 'Browse',
	                        width : 50,
	                        handler : function() {
	                          openPopup("relocationsiteidto", "");
	
	                       }
	
	                      }]
                }, 
                {
	                  xtype : 'combobox',
	                  fieldLabel : 'Reason',
	                  name : 'CR_ID',
	                  displayField : 'CHANGE_REASON',
	                  valueField : 'CR_ID',
	                  labelWidth : 130 / 1366 * screen.width,
	                  width : 325 / 1366 * screen.width,
	                  allowBlank : false,
	                  store : 'Administration.AssetChangeReason',
	                  margin : '5 10 10 10',
	                  listeners : {
		                    afterrender : function() {
		                      Ext.getStore('Administration.AssetChangeReason').removeAll();
		                      Ext.getStore('Administration.AssetChangeReason').getProxy().url = 'Services/DataMaster/AssetChangeReason/List?CHANGE_CATEGORY=RELOCATION_SITE';
		                      Ext.getStore('Administration.AssetChangeReason').load();
		                    }
	                  }
                },
	            {
		             xtype: 'fileuploadfield',
		             id: 'attachrelocationsite',
		             emptyText: 'Select a document to upload...',
		             name:'FILE',
		             fieldLabel: 'ATTACHMENT FILE',
		             buttonText: 'Browse',
		             labelWidth: 130/1366*screen.width,
		             width: 325/1366*screen.width,
		             margin:'5 10 10 10'
	            },
                {
	                  xtype : 'button',
	                  text : 'PROCESS',
	                  id : 'processrelocationsite',
	                  margin : '10 10 10 10',
	                  handler : function() {
	                    this.up('form').getForm().submit({
	                          waitMsg : 'Please Wait ....',
	                          success : function(form, action) {
	                          return_AssetChange(form, action,"assetrelocationsite");
	                          //this.up('form').getForm().reset();
	                          //refreshAssetChange('AssetChange.AssetChangeStore','./Services/AssetChange/Change/Detail/List?CHANGE_CATEGORY=RELOCATION_SITE');
	                          },
	                          failure : function(form, action) {  
	                        	return_AssetChange(form, action,"assetrelocationsite");
	              			    refreshAssetChange('AssetChange.AssetChangeStore','./Services/AssetChange/Change/Detail/List?CHANGE_CATEGORY=RELOCATION_SITE');
	              			    Ext.getStore('AssetDataMaster.AssetNonSac').getProxy().url = './Services/AssetChange/Asset/From/Site/List?site_id='+Ext.getCmp("relocationsiteid").getValue();
	              			    Ext.getStore('AssetDataMaster.AssetNonSac').load();
	              			    tempDataNE("assetrelocationsite","AssetDataMaster.AssetNonSac"); 
	                          }
	                        });
	                    
	                       
	                       
	                      }

                }, 
                {
	                  xtype : 'button',
	                  text : 'RESET',
	                  margin : '10 10 10 10',
	                  handler : function() {
	                    this.up('form').getForm().reset();
	                  }
                },
                {
              	  xtype :'panel',
              	  id    :'assetrelocationsite',
              	  border: false,
              	  
                }]

          },
          {
	            xtype : 'gridpanel',
	            region : 'center',
	            id : 'tablerelocationsite',
	            height : 590,
	            // width : 400,
	            titleAlign : 'center',
	            margin : '5 5 5 0',
	            border : true,
	            forceFit : true,
	            columnLines : true,
	            store : 'AssetChange.AssetChangeStore',
	            selType : 'checkboxmodel',
	            title : 'Relocation Site',
	            columns : [{dataIndex : 'change_category',text : 'CHANGE CATEGORY',align : 'left'},
	                       {dataIndex : 'change_reason',text : 'REASON',align : 'left'},
	                       {dataIndex : 'source_site_id',text : 'SOURCE SITE ID',align : 'left'},
	                       { dataIndex : 'destination',text : 'DESTINATION',align : 'left'}, 
	                       {
	                      	    xtype: 'actioncolumn',
	                      	    text : 'DOCUMENT',
	                      	    align : 'center',
	                      	    renderer: function (value, metadata, record) {
	                      	    	var document = record.get('document');
	                      	    	if (document=="null"){
	                      	    		this.items[0].icon = '';
	                      	    	}else{
	                      	    		var typefile=document.split(".");
	                      	    		 if (typefile[1]=="pdf"){ 
	                      	    			 this.items[0].icon = 'images/pdf-icon.png'; 
	                      	    		 }else if (typefile[1]=="docx" || typefile[1]=="doc"){
	                      	    			 this.items[0].icon = 'images/Word-icon.png';  
	                      	    		 }else if (typefile[1]=="xlsx" || typefile[1]=="xls"){ 
	                      	    			 this.items[0].icon = 'images/Excel-icon.png';  
	                      	    		 }
	                      	    	}
	                      	    },
	                      	    items: [{
	                      	        tooltip: 'Show Document',
	                      	        handler: function (grid, rowIndex, colIndex) {
	                      	        	openFIle(grid,rowIndex);
	                      	        }
	                      	    }]
	                      	}],
	            listeners : {
		              afterrender : function() {
		            	  refreshAssetChange('AssetChange.AssetChangeStore','./Services/AssetChange/Change/Detail/List?CHANGE_CATEGORY=RELOCATION_SITE');
		            	  console.log (hasPrivilege("NIM.SITE.ADD"));
		              },
		              
		              itemdblclick : function(grid, record) {
		            	  var change_id = record.get('change_id');
		            	  change_id_global = change_id;
		                		if(Ext.getCmp('panelrelocationsite').hidden == true) Ext.getCmp('panelrelocationsite').toggleCollapse(true);
		                		Ext.Ajax.request({
		                		    method: "GET",
		                		    dataType: "json",
		                		    url: "./Services/AssetChange/Change/Detail/"+change_id,
		                		    async: false	,
		                		    success: function(c) {
		                		    	var jsonResults= $.parseJSON(c.responseText);
		                		    	data = jsonResults.item[0];
		                		    	console.log (data);
		                		    	Ext.getCmp('panelrelocationsite').getForm().setValues(data);
		                		        }
		                		    });
		            	  Ext.getStore('AssetDataMaster.AssetNonSac').removeAll();
	                      Ext.getStore('AssetDataMaster.AssetNonSac').getProxy().url = './Services/AssetChange/Change/Details/List/'+change_id+'?start=0&limit=20';
	                      Ext.getStore('AssetDataMaster.AssetNonSac').load();
	                      ShowGridAsset("assetrelocationsite");      
		            	  	
		                }
	              
	            },
	
	            dockedItems : [
	                {
		                  xtype : 'pagingtoolbar',
		                  dock : 'bottom',
		                  store : 'AssetChange.AssetChangeStore',
		                  displayInfo : true
	                },
	                {
		                  xtype : 'toolbar',
		                  dock : 'top',
		                  items : [
		                  {
			                    xtype : 'textfield',
			                    fieldLabel : 'KEYWORD',
			                    id : 'keywordrelocationsite',
			                    margin : '0 10 0 10',
			                    labelWidth : 60,
			                    enableKeyEvents : true,
			                    listeners : {
			                      keypress : function(form, e) {
			                        if (e.getKey() == 13) {
			                        	 var keyword = Ext.getCmp("keywordrelocationsite").getValue();
				                    	 searchAssetChange('AssetChange.AssetChangeStore','./Services/AssetChange/Change/Detail/List?CHANGE_CATEGORY=RELOCATION_SITE&keyword=',keyword);
			                        }
			
			                      }
			                    }
		                  }, 
		                  {
			                    xtype : 'button',
			                    text : 'SEARCH',
			                    id : 'searchrelocationsite',
			                    margin : '0 30 0 0',
			                    handler : function() {
			                    	 var keyword = Ext.getCmp("keywordrelocationsite").getValue();
			                    	 searchAssetChange('AssetChange.AssetChangeStore','./Services/AssetChange/Change/Detail/List?CHANGE_CATEGORY=RELOCATION_SITE&keyword=',keyword);
			                   }
		                  },
		                  {
			                    xtype : 'button',
			                    text : 'ADD NEW',
			                    margin : '0 20 0 0',
			                    handler : function() {
			                      if (Ext.getCmp('panelrelocationsite').hidden == true)
			                        Ext.getCmp('panelrelocationsite').toggleCollapse(true);
			                   },
//			                   listeners : {
//			 		              afterrender : function() {
//			 		            	DefinePrivillage("NIM.SITE.VIEW",this);
//			 		              },
//			                   }
//		
		                  }, 
		                  {
			                    xtype : 'button',
			                    text : 'DELETE',
			                    margin : '0 20 0 0'
		
		                  },
		                  {
			                    xtype : 'button',
			                    text : 'REFRESH',
			                    margin : '0 20 0 0',
			                    handler : function() {
			                    	refreshAssetChange('AssetChange.AssetChangeStore','./Services/AssetChange/Change/Detail/List?CHANGE_CATEGORY=RELOCATION_SITE');
			                    }
		                  }]
		                }
	                ]

          }]
    });

